#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "sgl.h"
#include "sgl_draw_rect.h"
#include "sgl_draw_circle.h"
#include "sgl_draw_misc.h"
#include "sgl_draw_arc.h"


int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
extern char *fbp;
int x = 0, y = 0;
long int location = 0;

int main()
{


    // Open the file for reading and writing
    fbfd = open("/dev/fb0", O_RDWR);
    if (fbfd == -1) {
        perror("Error: cannot open framebuffer device");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");

    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("Error reading fixed information");
        exit(2);
    }

    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("Error reading variable information");
        exit(3);
    }

    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);

    // Figure out the size of the screen in bytes
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    // Map the device to memory
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
    if ((int)fbp == -1) {
        perror("Error: failed to map framebuffer device to memory");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully.\n");

    x = 100; y = 100;       // Where we are going to put the pixel

    // Figure out where in memory to put the pixel
    uint16_t *ppp=(uint16_t *)fbp;
    for (y = 0; y < 320; y++)
        for (x = 0; x < 480; x++) {
            *ppp = 0xffff;
            ppp ++;
    }

    sgl_rect_t rect;
    rect.x1 = 20;
    rect.x2 = 300;
    rect.y1 = 50;
    rect.y2 = 60;

    sgl_rect_t rect2;
    rect2.x1 = 20;
    rect2.x2 = 30;
    rect2.y1 = 50;
    rect2.y2 = 60;

    sgl_draw_round_rect_solid(rect, 5, 0xff00, 0xffff); //矩形颜色为红色，背景为白色
    sgl_draw_round_rect_solid(rect2, 5, 0xff, 0xffff); //矩形颜色为红色，背景为白色
    

    sgl_rect_t red;
    red.x1 = 120;
    red.x2 = 140;
    red.y1 = 120;
    red.y2 = 140;
    sgl_rect_t green;
    green.x1 = 140;
    green.x2 = 160;
    green.y1 = 140;
    green.y2 = 160;
    sgl_rect_t blue;
    blue.x1 = 160;
    blue.x2 = 180;
    blue.y1 = 160;
    blue.y2 = 180;
    sgl_draw_round_rect_solid(red, 5, 0x1f<<11, 0xffff);
    sgl_draw_round_rect_solid(green, 5, 0x3f<<5, 0xffff);
    sgl_draw_round_rect_solid(blue, 5, 0x1f, 0xffff);

    sgl_rect_t mix;
    mix.x1 = 0;
    mix.x2 = 80;
    mix.y1 = 80;
    mix.y2 = 160;

    sgl_draw_round_rect_solid(mix, 20, 31743, 0xffff);


    sgl_draw_circle_solid(200,200,100,0x1f, 0xffff);

    while (1)
    {
        if(rect2.x2 < 300) {
            rect2.x1+=1;
            rect2.x2+=1;
            sgl_draw_round_rect_solid_rhalf(rect2, 5, 0xff, 0xff00);
            usleep(1000);
        }
        else {
            rect2.x1=20;
            rect2.x2=30;
            sgl_draw_round_rect_solid(rect, 5, 0xff00, 0xffff); //矩形颜色为红色，背景为白色
            sgl_draw_round_rect_solid(rect2, 5, 0xff, 0xffff);  //矩形颜色为红色，背景为白色
            rect2.x1=25;
            rect2.x2=35;
        }
    }

    munmap(fbp, screensize);
    close(fbfd);
    return 0;
}
